package com.cetsk.android.talkorigins;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;

public class HomeActivity extends ClaimsListActivity {
	private ProgressDialog dialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		dialog = ProgressDialog.show(this,"Loading", "Building database. Please wait....",true);
		new Thread() {
			public void run() {
				try{
					new HTMLParse(getApplicationContext());
				} catch (Exception e) { }
				handler.sendEmptyMessage(0);
				dialog.dismiss(); 
			}
		}.start();
		super.onCreate(savedInstanceState);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			ClaimAdapter ca = new ClaimAdapter(getBaseContext(), R.layout.list_item, ClaimHelper.getClaims(getApplicationContext(),0, true));
			getListView().setAdapter(ca);
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle("Quit?")
			.setMessage("Are you sure you want to quit?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					HomeActivity.this.finish();    
				}
			})
			.setNegativeButton("No", null)
			.show();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
}